// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Motobug Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value1 : object.startPos.x
private alias object.value2 : object.startPos.y
private alias object.value3 : object.exhaustTimer

private alias 0 : MOTOBUG_MOVING
private alias 1 : MOTOBUG_PAUSED

private alias 0 : MOTOBUG_ANI_MOVING
private alias 1 : MOTOBUG_ANI_IDLE

// Player Aliases
private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom

// Path ID Aliases
private alias 0 : PATH_A


// ========================
// Function Declarations
// ========================

reserve function Motobug_DebugDraw
reserve function Motobug_DebugSpawn


// ========================
// Function Definitions
// ========================

private function Motobug_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


private function Motobug_DebugSpawn
	CreateTempObject(TypeName[Motobug], 0, object.xpos, object.ypos)
	GetBit(temp0, object.direction, 0)
	if temp0 == FLIP_NONE
		object[tempObjectPos].direction = FLIP_NONE
		object[tempObjectPos].xvel = -0x40000
	else
		object[tempObjectPos].direction = FLIP_X
		object[tempObjectPos].xvel = 0x40000
	end if
end function


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case MOTOBUG_MOVING
		object.priority = PRIORITY_ACTIVE

		object.xpos += object.xvel

		// Check object collision
		ObjectTileGrip(CSIDE_FLOOR, 0, 15, PATH_A)
		if checkResult == false
			// If on the edge, then pause to turn around
			object.state = MOTOBUG_PAUSED
			object.animation = MOTOBUG_ANI_IDLE
		end if


		// Update exhaust process
		if object.exhaustTimer < 20
			object.exhaustTimer++
		else
			object.exhaustTimer = 0

			// Create the exhaust object
			CreateTempObject(TypeName[Motobug Exhaust], 0, object.xpos, object.ypos)
			
			// Offset the exhaust based on the Motobug's direction
			if object.direction == FLIP_NONE
				object[tempObjectPos].xpos += 0x160000
			else
				object[tempObjectPos].xpos -= 0x160000
			end if

			// Set the exhaust to match the Motobug's direction
			object[tempObjectPos].direction = object.direction
		end if
		break

	case MOTOBUG_PAUSED
		if object.timer < 0
			// Stay still for a moment...
			object.timer++
		else
			// Start moving again
			object.timer = 0

			object.state = MOTOBUG_MOVING
			object.animation = MOTOBUG_ANI_MOVING

			// Flip the Motobug
			object.direction ^= FLIP_X
			FlipSign(object.xvel)
		end if
		break

	end switch

	if object.outOfBounds == true
		temp0 = object.xpos
		temp1 = object.ypos

		object.xpos = object.startPos.x
		object.ypos = object.startPos.y

		if object.outOfBounds == true
			if object.propertyValue == 0
				object.direction = FLIP_NONE
				object.xvel = -0x40000
			else
				object.direction = FLIP_X
				object.xvel = 0x40000
			end if

			object.animation = MOTOBUG_ANI_MOVING
			object.priority = PRIORITY_BOUNDS
			object.state = MOTOBUG_MOVING
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

	// Check collision with players
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -14, -14, 14, 14, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		if checkResult == true
			CallFunction(Player_BadnikBreak)
		end if
	next

	if object.animation == MOTOBUG_ANI_MOVING
		object.frame = object.animationTimer
		object.frame >>= 3
		object.animationTimer++
		object.animationTimer &= 31
	end if
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	// Check which sprites to load based on current folder
	CheckCurrentStageFolder("Zone01")
#platform: USE_ORIGINS
	// Motobugs are big fans of the phrase "being in Mission Mode"
	temp0 = checkResult
	CheckEqual(game.playMode, BOOT_PLAYMODE_MISSION)
	checkResult |= temp0
#endplatform
	if checkResult == false
		// Motobug in GHZ (or a mission) - load GHZ frames
		LoadSpriteSheet("WZ/Objects.gif")
		SpriteFrame(-20, -13, 37, 28, 105, 18)
		SpriteFrame(-20, -13, 37, 28, 67, 18)
		SpriteFrame(-20, -13, 37, 28, 105, 18)
		SpriteFrame(-20, -13, 37, 28, 67, 18)
	end if

	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		// Motobug in MBZ - load MBZ frames
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-21, -13, 40, 28, 170, 145)
		SpriteFrame(-20, -14, 39, 29, 171, 174)
		SpriteFrame(-20, -13, 39, 28, 171, 204)
		SpriteFrame(-20, -14, 39, 29, 171, 174)
	end if

	// Setup all Motobugs
	foreach (TypeName[Motobug], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos

		// Set their direction based on their propertyValue
		if object[arrayPos0].propertyValue == 0
			object[arrayPos0].direction = FLIP_NONE
			object[arrayPos0].xvel = -0x40000
		else
			object[arrayPos0].direction = FLIP_X
			object[arrayPos0].xvel = 0x40000
		end if
	next

	// Add Motobug to the debug item list
	SetTableValue(TypeName[Motobug], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Motobug_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Motobug_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		case 0 // direction
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		case 0 // direction
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(object.direction, object.propertyValue, 0)
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	// Check which sprites to load based on current folder
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		// Motobug in in MBZ - load MBZ frames
		
                LoadSpriteSheet("WZ/Objects.gif")
		SpriteFrame(-20, -13, 37, 28, 105, 18)
	else
		// Thanks to Origins using Motobugs everywhere, just load the GHZ sheet as a fallback, even if not in GHZ
		
		LoadSpriteSheet("GHZ/Objects.gif")
		SpriteFrame(-21, -13, 40, 28, 98, 127)
	end if

	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
end event
